/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.notify.service;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.notify.dao.NotiUserOnlineDao;
import com.thinkgem.jeesite.modules.notify.entity.NotiApp;
import com.thinkgem.jeesite.modules.notify.entity.NotiUserOnline;
import com.thinkgem.jeesite.modules.notify.service.dto.NotiUserOnlineRes;
import com.thinkgem.jeesite.modules.notify.service.dto.NotiUserOnlineServiceODTO;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 推送用户在线图表Service
 *
 * @author hf
 * @version 2018-12-24
 */
@Service
@Transactional(readOnly = true)
public class NotiUserOnlineService extends CrudService<NotiUserOnlineDao, NotiUserOnline> {

    @Autowired
    private NotiUserOnlineDao notiUserOnlineDao;

    public NotiUserOnline get(String id) {
        return super.get(id);
    }

    public List<NotiUserOnline> findList(NotiUserOnline notiUserOnline) {
        return super.findList(notiUserOnline);
    }

    public Page<NotiUserOnline> findPage(Page<NotiUserOnline> page, NotiUserOnline notiUserOnline) {
        return super.findPage(page, notiUserOnline);
    }

    @Transactional(readOnly = false)
    public void save(NotiUserOnline notiUserOnline) {
        super.save(notiUserOnline);
    }

    @Transactional(readOnly = false)
    public void delete(NotiUserOnline notiUserOnline) {
        super.delete(notiUserOnline);
    }

    public NotiUserOnline getNotiUserOnlineByUserName(String userName, String appId) {
        return notiUserOnlineDao.getNotiUserOnlineByUserName(userName, appId);
    }

    public List<NotiApp> getApp() {
        //当前登录人只能看到自己的app
        User user = UserUtils.getUser();
        if (StringUtils.isNotBlank(user.getId())) {
            return notiUserOnlineDao.getApp(user.getId());
        }
        return new ArrayList<NotiApp>();
    }

    public NotiUserOnlineServiceODTO getAppUserOnline() {
        //当前登录人只能看到自己的app
        List<NotiApp> app = this.getApp();
        if (app.size() <= 0) {
            return new NotiUserOnlineServiceODTO();
        }
        NotiUserOnlineServiceODTO onlineServiceODTO = new NotiUserOnlineServiceODTO();
        List appName = new ArrayList();
        List appOlineNum = new ArrayList();

        for (NotiApp notiApp : app) {
            List appOlineNums = new ArrayList();
            //取每个app的在线人数
            appName.add(notiApp.getAppName());
            NotiUserOnlineRes online = notiUserOnlineDao.getOnline(notiApp.getId());
            if (online != null) {
                appOlineNums.add(online.getA());
                appOlineNums.add(online.getB());
                appOlineNums.add(online.getC());
                appOlineNums.add(online.getD());
                appOlineNums.add(online.getE());
                appOlineNums.add(online.getF());
                appOlineNums.add(online.getG());
                appOlineNums.add(online.getH());
                appOlineNums.add(online.getI());
                appOlineNums.add(online.getJ());
                appOlineNums.add(online.getK());
                appOlineNums.add(online.getL());
                appOlineNums.add(online.getM());
                appOlineNums.add(online.getN());
                appOlineNums.add(online.getO());
                appOlineNums.add(online.getP());
                appOlineNums.add(online.getQ());
                appOlineNums.add(online.getW());
                appOlineNums.add(online.getR());
                appOlineNums.add(online.getS());
                appOlineNums.add(online.getT());
                appOlineNums.add(online.getX());
                appOlineNums.add(online.getY());
                appOlineNums.add(online.getZ());
            }
            appOlineNum.add(appOlineNums);
        }
        onlineServiceODTO.setAppName(appName);
        onlineServiceODTO.setAppOnlineNum(appOlineNum);
        return onlineServiceODTO;
    }

}